var imgPath = '';
var tickerSeperator = '####';
var tck_News = new Array('');


var tickerobj ;
var tickerTitle;
var breakingNewsDelay = 5000;
var breakingNewsColor = "#be0303";
var normalNewsColor = "#000";
var normalNewsDelay = 3000;
var breakingNewsCount = 5;
var numberOfNews = 30;
var currentNews = "breaking";

var tck_chrPos = 0; // Starting character position
var tck_strPos = 0; // Starting string position

var tck_chrPeriod = 50; // Delay between characters = 0.1 secs
var tck_strPeriod; // Delay between strings = 2 secs

var tck_chrTimer = null; // Timer to show next character
var tck_strTimer = null; // Timer to show next string
var tck_timerID = null;
var tck_rTimer = null;

var tck_MainLoop = 10; // Number of strings in main loop
var tck_RunningTimer = "c" // To flag which timer is running: c / s


function tck_ShowNextChr()
{
    if (tck_News.length == 0) return;
    var currStr = tck_News[tck_strPos]; // The current string
	if(currStr && tck_chrPos <= currStr.length) // As long as we have not passed the end of the string
	{
        tck_RunningTimer = "c";
        
        tickerobj.innerHTML = currStr.substr(0, tck_chrPos); // write from beginning to current chr position

        tck_chrPos++; // Increment chr position
        tck_chrTimer = setTimeout("tck_ShowNextChr()", tck_chrPeriod); // call this function again
	}
	else // We've reached the end of the current string, so stop showing chrs, and move to next string
	{
		 tck_RunningTimer = "s";
		 tck_chrPos = 0; // Reset back to starting character position as we will move to next string now
		 tck_strTimer = setTimeout("tck_ShowNextStr()", tck_strPeriod);
	}
}

function tck_clearTimers()
{
	if (tck_rTimer)
		clearTimeout(tck_rTimer);
	
	if (tck_chrTimer)
		clearTimeout(tck_chrTimer);
	
	if (tck_strTimer)
		clearTimeout(tck_strTimer);
}

function tck_Resume()
{
	tck_clearTimers();
	
	tck_strPos = ((tck_strPos < tck_News.length - 1) && (tck_strPos < tck_MainLoop - 1)) ? (tck_strPos + 1) : 0;
	setCss();
	tck_chrPos = 0; // Reset back to starting character position as we will move to next string now
	
	setTimeout("tck_ShowNextChr()", tck_chrPeriod);
}

function tck_Pause()
{
	var currStr = tck_News[tck_strPos];
	if (currStr == undefined)
		return;
	
	tck_clearTimers();
	
	tickerobj.innerHTML = currStr;
}

function tck_ShowNextStr()
{
	if (tck_News.length == 0)
		return;
	
	 // Check if position is at end of available items or end of items to show
	 
	 tck_strPos = ((tck_strPos < tck_News.length - 1) && (tck_strPos < tck_MainLoop - 1)) ? (tck_strPos + 1) : 0;
	 setCss();
	 tck_chrTimer = setTimeout("tck_ShowNextChr()", tck_chrPeriod); // In either case, start showing chrs again
}

function tck_showPrev()
{
    tck_strPos = (tck_strPos > 0) ? (tck_strPos - 1) : (tck_News.length - 1);
    setCss();

    tck_Pause();

    tck_rTimer = setTimeout("tck_Resume()", 10000);
}

function tck_showNext() // called when the Next button is clicked
{
	tck_strPos = ((tck_strPos < tck_News.length - 1) && (tck_strPos < tck_MainLoop - 1)) ? (tck_strPos + 1) : 0;
    setCss();
	tck_Pause();
	
	tck_rTimer = setTimeout("tck_Resume()", 10000);
}


function initTicker()
{
    tck_News = GetTickerNewsArray();
    tickerobj = document.getElementById('ticker');
    tickerTitle = document.getElementById('tickerTitle');
    
    tck_MainLoop  		= tck_News.length;
    tck_strPeriod 		= breakingNewsDelay;
    //setCss();

    tck_chrTimer 		= setTimeout("tck_ShowNextChr()", tck_chrPeriod); // Start the timer
}

function GetTickerNewsArray()
{
    var tickerNewsObject = tickerJson;
    var tickerArray = new Array();
    if(typeof tickerNewsObject == "object")
    {
        if(!isEmpty(tickerNewsObject['general']))
        {
            numberOfNews = (tickerNewsObject['general'].length < numberOfNews) ?
                                    tickerNewsObject['general'].length : numberOfNews;
            for(var i = 0; i < numberOfNews; i++)
            {
                tickerArray[i] = tickerNewsObject['general'][i].description;
            }
        }
    }
    
    return tickerArray;
}

function isEmpty(object)
{
    for(var i in object)
    {
        return false;
    }
    return true;
}

function setCss()
{
    if(tck_strPos < breakingNewsCount && currentNews != "breaking")
    {
        currentNews = "breaking";
        tck_strPeriod = breakingNewsDelay;
        tickerobj.style.color = breakingNewsColor;
        tickerTitle.className = 'tazatareen';
    }
    else if(tck_strPos >= breakingNewsCount && currentNews != "normal")
    {
        currentNews = "normal";
        tck_strPeriod = normalNewsDelay;
        tickerobj.style.color = normalNewsColor;
        tickerTitle.className = 'surkhian';
    } 
}


